//
//  KKOrderCollectionViewCell.m
//  FreightAssistant
//
//  Created by Mac mini on 15/12/24.
//  Copyright © 2015年 leo. All rights reserved.
//

#import "KKSupplyOrderCollectionViewCell.h"

@implementation KKSupplyOrderCollectionViewCell

- (void)awakeFromNib {
    // Initialization code
    
    [self updateCellUIWithItemState: 0];
}

#pragma mark - Private methods

- (void)updateCellUIWithItemState:(NSInteger)state {
    
    _kk_orderStateButton.hidden = false;
    
    switch (state) {
        case KKOrderItemStateIsCreate:
        {
            [_kk_orderStateTags0 setImage: [UIImage imageNamed: @"orderState_selected"]];
            [_kk_orderStateTags1 setImage: [UIImage imageNamed: @"orderState_normal"]];
            [_kk_orderStateTags2 setImage: [UIImage imageNamed: @"orderState_normal"]];
            [_kk_orderStateTags3 setImage: [UIImage imageNamed: @"orderState_normal"]];
            [_kk_orderStateTags4 setImage: [UIImage imageNamed: @"orderState_normal"]];
            
            [_kk_orderStateLabel0 setTextColor: [UIColor colorWithHexString: @"2eb1e7"]];
            [_kk_orderStateLabel1 setTextColor: [UIColor colorWithHexString: @"c5c5c5"]];
            [_kk_orderStateLabel2 setTextColor: [UIColor colorWithHexString: @"c5c5c5"]];
            [_kk_orderStateLabel3 setTextColor: [UIColor colorWithHexString: @"c5c5c5"]];
            [_kk_orderStateLabel4 setTextColor: [UIColor colorWithHexString: @"c5c5c5"]];
            
            [_kk_orderStateButton setTitle: @"已支付" forState: UIControlStateNormal];
        }
            break;
        case KKOrderItemStateIsPaid:
        {
            [_kk_orderStateTags0 setImage: [UIImage imageNamed: @"orderState_selected"]];
            [_kk_orderStateTags1 setImage: [UIImage imageNamed: @"orderState_selected"]];
            [_kk_orderStateTags2 setImage: [UIImage imageNamed: @"orderState_normal"]];
            [_kk_orderStateTags3 setImage: [UIImage imageNamed: @"orderState_normal"]];
            [_kk_orderStateTags4 setImage: [UIImage imageNamed: @"orderState_normal"]];
            
            [_kk_orderStateLabel0 setTextColor: [UIColor colorWithHexString: @"2eb1e7"]];
            [_kk_orderStateLabel1 setTextColor: [UIColor colorWithHexString: @"2eb1e7"]];
            [_kk_orderStateLabel2 setTextColor: [UIColor colorWithHexString: @"c5c5c5"]];
            [_kk_orderStateLabel3 setTextColor: [UIColor colorWithHexString: @"c5c5c5"]];
            [_kk_orderStateLabel4 setTextColor: [UIColor colorWithHexString: @"c5c5c5"]];
            
            [_kk_orderStateButton setTitle: @"准备中" forState: UIControlStateNormal];
        }
            break;
        case KKOrderItemStateIsReady:
        {
            [_kk_orderStateTags0 setImage: [UIImage imageNamed: @"orderState_selected"]];
            [_kk_orderStateTags1 setImage: [UIImage imageNamed: @"orderState_selected"]];
            [_kk_orderStateTags2 setImage: [UIImage imageNamed: @"orderState_selected"]];
            [_kk_orderStateTags3 setImage: [UIImage imageNamed: @"orderState_normal"]];
            [_kk_orderStateTags4 setImage: [UIImage imageNamed: @"orderState_normal"]];
            
            [_kk_orderStateLabel0 setTextColor: [UIColor colorWithHexString: @"2eb1e7"]];
            [_kk_orderStateLabel1 setTextColor: [UIColor colorWithHexString: @"2eb1e7"]];
            [_kk_orderStateLabel2 setTextColor: [UIColor colorWithHexString: @"2eb1e7"]];
            [_kk_orderStateLabel3 setTextColor: [UIColor colorWithHexString: @"c5c5c5"]];
            [_kk_orderStateLabel4 setTextColor: [UIColor colorWithHexString: @"c5c5c5"]];
            
            [_kk_orderStateButton setTitle: @"发货中" forState: UIControlStateNormal];
        }
            break;
        case KKOrderItemStateIsSend:
        {
            [_kk_orderStateTags0 setImage: [UIImage imageNamed: @"orderState_selected"]];
            [_kk_orderStateTags1 setImage: [UIImage imageNamed: @"orderState_selected"]];
            [_kk_orderStateTags2 setImage: [UIImage imageNamed: @"orderState_selected"]];
            [_kk_orderStateTags3 setImage: [UIImage imageNamed: @"orderState_selected"]];
            [_kk_orderStateTags4 setImage: [UIImage imageNamed: @"orderState_normal"]];
            
            [_kk_orderStateLabel0 setTextColor: [UIColor colorWithHexString: @"2eb1e7"]];
            [_kk_orderStateLabel1 setTextColor: [UIColor colorWithHexString: @"2eb1e7"]];
            [_kk_orderStateLabel2 setTextColor: [UIColor colorWithHexString: @"2eb1e7"]];
            [_kk_orderStateLabel3 setTextColor: [UIColor colorWithHexString: @"2eb1e7"]];
            [_kk_orderStateLabel4 setTextColor: [UIColor colorWithHexString: @"c5c5c5"]];

            [_kk_orderStateButton setTitle: @"已完成" forState: UIControlStateNormal];
        }
            break;
        case KKOrderItemStateIsFinish:
        {
            [_kk_orderStateTags0 setImage: [UIImage imageNamed: @"orderState_selected"]];
            [_kk_orderStateTags1 setImage: [UIImage imageNamed: @"orderState_selected"]];
            [_kk_orderStateTags2 setImage: [UIImage imageNamed: @"orderState_selected"]];
            [_kk_orderStateTags3 setImage: [UIImage imageNamed: @"orderState_selected"]];
            [_kk_orderStateTags4 setImage: [UIImage imageNamed: @"orderState_selected"]];
            
            [_kk_orderStateLabel0 setTextColor: [UIColor colorWithHexString: @"2eb1e7"]];
            [_kk_orderStateLabel1 setTextColor: [UIColor colorWithHexString: @"2eb1e7"]];
            [_kk_orderStateLabel2 setTextColor: [UIColor colorWithHexString: @"2eb1e7"]];
            [_kk_orderStateLabel3 setTextColor: [UIColor colorWithHexString: @"2eb1e7"]];
            [_kk_orderStateLabel4 setTextColor: [UIColor colorWithHexString: @"2eb1e7"]];
            
            _kk_orderStateButton.hidden = true;
        }
            break;
        default:
            break;
    }
    
    
}

#pragma mark - Setter methods

- (void)setKk_currentOrderItem:(KKSupplyOrderItem *)kk_currentOrderItem {
    
    _kk_currentOrderItem = kk_currentOrderItem;
    
    NSInteger state = _kk_currentOrderItem.orderItemState;
    [self updateCellUIWithItemState: state];
    
    _kk_createTimeLabel.text = [_kk_currentOrderItem kk_createTime];
    _kk_amountLabel.text = [NSString stringWithFormat: @"¥ %.2f", [_kk_currentOrderItem.kk_amount doubleValue] + [_kk_currentOrderItem.kk_logisticsAmount doubleValue]];
    _kk_orderInfoLabel.text = [_kk_currentOrderItem orderDescription];
    
    [_kk_orderNumberButton setTitle: [NSString stringWithFormat: @"快递单号:%@", _kk_currentOrderItem.kk_logisticsNumber] forState: UIControlStateNormal];
}

#pragma mark - Action methods
- (IBAction)actionOfDetail:(UIButton *)sender {
    
    if (_eventBlock) {
        _eventBlock(KKSupplyOrderCollectionEventTypeIsDetail);
    }
}

- (IBAction)actionOfEdit:(UIButton *)sender {
    
    NSInteger state = _kk_currentOrderItem.orderItemState;
    
    __weak typeof(_kk_currentOrderItem) weakItem = _kk_currentOrderItem;
    __weak typeof(self) weakSelf = self;
    
    [KKDataBaseAPIInterface updateSupplyOrderStateWithIdentifier: _kk_currentOrderItem.kk_identifier state: state + 1 successful:^(id object) {
        NSLog(@"object = %@", object);
        [weakItem setKk_state: @(state + 1)];
        [weakSelf updateCellUIWithItemState: state + 1];
        
    } faile:^(NSString *error) {
        NSLog(@"error = %@", error);
    }];
    
    
}
- (IBAction)actionOfLogisticsInfo:(UIButton *)sender {
    
    if (_eventBlock) {
        _eventBlock(KKSupplyOrderCollectionEventTypeIsLogisticsDetail);
    }
}
@end
